﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;

namespace Stock.Util
{
    public static class ProcessUtil
    {
        public static void Start(string path, string args = null)
        {
            if (string.IsNullOrEmpty(path))
            {
                return;
            }
            try
            {
                string name = Path.GetFileNameWithoutExtension(path);
                var p = GetProcess(name);
                if (p == null)
                {
                    _ = Process.Start(path, args);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        public static void Kill(string name)
        {
            var p = GetProcess(name);
            if (p != null)
            {
                p.Kill();
            }
        }

        public static Process GetProcess(string name)
        {
            var ps = Process.GetProcessesByName(name);
            if (ps != null && ps.Length > 0)
            {
                return ps.FirstOrDefault();
            }
            return null;
        }
    }
}
